package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("UserOrderController")
@Slf4j
@Api(tags = "订单管理相关接口")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @ApiOperation("订单接口")
    /**
     * 下单
     */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> OrderSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("订单的信息:{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO=orderService.insertOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @ApiOperation("历史订单查询")
    @GetMapping("/historyOrders")
    public Result<PageResult> listHistoryOd(int page, int pageSize, Integer status){
        PageResult pageResult= orderService.pageQuery(page,pageSize,status);
        return Result.success(pageResult);
    }

    /**
     * 根据订单id查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("根据订单id查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> DetailListById(@PathVariable Long id){
        log.info("用户要查询的订单id为:{}",id);
        OrderVO orderVO=orderService.getById(id);
        return Result.success(orderVO);
    }

    /**
     * 取消订单业务功能
     * @param id
     * @return
     * @throws Exception
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result CancelOrder(@PathVariable Long id)  throws Exception{
        log.info("要取消的订单id为:{}",id);
        orderService.cancelOrderById(id);
        return Result.success();
    }

    /**
     * 再来一单接口
     * @param id
     * @return
     */
    @ApiOperation("再来一单接口")
    @PostMapping("/repetition/{id}")
    public Result againOrder(@PathVariable Long id){
        orderService.againOrder(id);
        return Result.success();
    }

    @ApiOperation("用户催单")
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable("id") Long id){
        orderService.reminder(id);
        return Result.success();
    }

}
